findUserAddressListByUserId();

//获取用户地址列表
function findUserAddressListByUserId() {
    localStorage.setItem("selectAddress", "");
    var page = 0;
    var size = 5;
    var sort = "id";
    var userId = "1";
    var obj = JSON.stringify({
        "userId": userId,
    });

    httpHelperpost(_API_USER_ADDRESSLIST + "?page=" + page + "&size=" + size + "&sort=" + sort, obj, function (isSuccess, data) {
        if (isSuccess == true) {
            var response = data.responseText;
            var value = JSON.parse(response);

            console.log(response);
            var addHtml = ""
            var userId = 1;
            var province = "";
            var city = "";
            var district = "";
            var address = "";
            var sex = "";
            for (var i = 0; i < value.data.content.length; i++) {
                province = value.data.content[i].province;
                city = value.data.content[i].city;
                district = value.data.content[i].district;
                address = value.data.content[i].address;
                longitude = value.data.content[i].longitude;
                latitude = value.data.content[i].latitude;
                value.data.content[i].sex == 0 ? sex = "先生" : sex = "女士";


                addHtml += "<li class='address_every' data-addressId=" + value.data.content[i].id + " data-province=" + province + " data-city=" + city + " data-district=" + district + " data-lat=" + latitude + " data-lng=" + longitude + ">"
                    + "<div><span class='address_spe'>" + province + city + district + "<span class='address_spe_adr'>" + address + "</span></span>";
                if (value.data.content[i].tag == 1) {
                    addHtml += "<span class='address_tag address_tag_home'>家</span>";
                } else if (value.data.content[i].tag == 2) {
                    addHtml += "<span class='address_tag address_tag_company'>公司</span>";
                } else {

                    addHtml += "<span class='address_tag address_tag_school'>学校</span>";
                }

                addHtml += "</div>"
                    + "<div class='address_det'>" + value.data.content[i].houseNumber + "</div>"
                    + "<div class='address_infor'><span class='consignee'>" + value.data.content[i].consignee + "</span>&nbsp&nbsp(<span class='sex'>" + sex + "</span>)&nbsp&nbsp<span class='phone'>" + value.data.content[i].phone + "</span></div>"
                    + "<span class='address_editor'><img src='./img/editor_01.png' onclick='updateDis(event,this)'></span>"
                    + "<span class='address_delete'><img onclick='delUserAddress(this,event);' src='./img/XX_01.png'></span>"
                    + "</li>";
            }

            $(".address").html(addHtml);

            if (value.code == 0) {
                console.log("查询成功");
                selectAddress();
            } else if (value.code == 401) {
                alert(value.message);
                window.location.href = './index.html';
            } else {
                alert(value.message);
                console.log("查询失败");
            }
        } else {
        }
    });
}

//删除地址
function delUserAddress(that, e) {
    // e.stopPropagation();
    var userId = "1";
    var addressId = $(that).parents(".address_every").attr("data-addressId");

    var obj = JSON.stringify({
        "userId": userId,
        "id": addressId,
    });
    httpHelperpost(_API_DEL_USER_ADDRESS, obj, function (isSuccess, data) {
        if (isSuccess == true) {
            var response = data.responseText;
            var value = JSON.parse(response);

            console.log(response);


            if (value.code == 0) {
                console.log("删除成功");
                $(that).parents("li").eq(0).remove();
            } else if (value.code == 401) {
                alert(value.message);
                window.location.href = './index.html';
            } else {
                alert(value.message);
                console.log("删除失败");
            }
        } else {
        }
    });
}

//点击跳转修改到用户地址并传递参数
function updateDis(e, that) {
    // e.stopPropagation();
    localStorage.setItem("province", $(that).parents(".address_every").attr("data-province"));
    localStorage.setItem("city", $(that).parents(".address_every").attr("data-city"));
    localStorage.setItem("district", $(that).parents(".address_every").attr("data-district"));
    localStorage.setItem("address", $(that).parents(".address_every").find(".address_spe_adr").text());

    var addressId = $(that).parents(".address_every").attr("data-addressId");
    var consignee = $(that).parents(".address_every").find(".consignee").text();
    var phone = $(that).parents(".address_every").find(".phone").text();
    var longitude = $(that).parents(".address_every").attr("data-lng");
    var latitude = $(that).parents(".address_every").attr("data-lat");
    var sex = 0;
    var tag = 0;
    var houseNumber = $(that).parents(".address_every").find(".address_det").text();
    $(that).parents(".address_every").find(".sex").text() == "先生" ? sex = 0 : sex = 1;
    if ($(that).parents(".address_every").find(".address_tag").text() == "家") {
        tag = 1;
    } else if ($(that).parents(".address_every").find(".address_tag").text() == "公司") {
        tag = 2;
    } else {
        tag = 3;
    }
    window.location.href = './addressNew.html?addressId=' + addressId + '&consignee=' + consignee + '&phone=' + phone + '&longitude=' + longitude + '&latitude=' + latitude + '&sex=' + sex + '&tag=' + tag + '&houseNumber=' + houseNumber;
}

//获取选中地址
function selectAddress() {
    var Request = new Object();
    Request = GetRequest();
    var select = Request['select'];
    if (select == undefined) {
        $(".headerText").css("display", "block");
        $(".headerReturn").click(function () {
            window.location.href = './myCenter.html';
        });
    } else {
        $(".headerText").css("display", "none");
        $(".headerReturn").click(function () {
            window.history.back(-1);
        });
        $(".address li").click(function () {
            localStorage.setItem("selectAddress", $(this).find(".address_spe .address_spe_adr").text());
            localStorage.setItem("selectAddressId", $(this).attr("data-addressid"));
            window.history.back(-1);
        });
    }
}

//获取url中"?"符后的字串
function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);

        strs = str.split("&");

        for (var i = 0; i < strs.length; i++) {

            theRequest[strs[i].split("=")[0]] = strs[i]
                .split("=")[1];
        }
    }
    return theRequest;
}